package com.aaa.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

/**
 * @author ：zhouhangyong
 * @date ：Created in 2022/6/15 9:53
 * @description：动态代理基于接口
 * @modified By：
 * @version:
 */
public class ProxyDynamicImpl<T> implements ProxyDynamic<T>, InvocationHandler {
    private T targetObj;
    public ProxyDynamicImpl(T targetObj){
        this.targetObj = targetObj;
    }
    public ProxyDynamicImpl(){}
    @Override
    public T getProxyObj() {
        Object proxyInstance = Proxy.newProxyInstance(targetObj.getClass().getClassLoader(), targetObj.getClass().getInterfaces(), this);
        return (T) proxyInstance;
    }
    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        System.out.println("动态代理日志："+method.getName()+new Date());
        //执行目标对象中的方法
        Object invoke = method.invoke(targetObj, objects);
        return invoke;
    }

    public static void main(String[] args) {
        ProxyDynamic<MouseBuyService> proxyDynamic = new ProxyDynamicImpl<MouseBuyService>(new MouseProxyServiceImpl());
        MouseBuyService proxyObj = proxyDynamic.getProxyObj();
        proxyObj.byMouse(800);

    }
}
